package com.ruoyi.system.controller;

import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;

/**
 * 企业微信通讯录 - 工具类
 */
public class CorpWechatContactHelper extends CorpWechatAppHelper {

	public CorpWechatContactHelper(String corpid, String corpsecret) {
		super(corpid, corpsecret);
	}
	
	/*
	 * 获取全量部门列表
	 * 
	 * @url https://developer.work.weixin.qq.com/document/path/90208
	 */
	public JSONArray getAllDepartment() {
		String accessToken = getAccessToken();
		String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
		String param = String.format("access_token=%s", accessToken);
		
		String response = HttpUtils.sendGet(url, param);
		System.out.println(response);
		JSONObject json = JSONObject.parseObject(response);
		
		if(json.getInteger("errcode") == 0) {
			return json.getJSONArray("department");
		}
		
		return null;
	}
	
	/*
	 * 获取部门下的全量成员（递归子部门）
	 * 
	 * @url https://developer.work.weixin.qq.com/document/path/90200
	 * 
	 * {
     *     "userid": "zhangsan",
     *     "name": "张三",
     *     "department": [1, 2],
	 *     "open_userid": "xxxxxx"
     * }
	 */
	public JSONArray getDeptAllPerson(int deptId) {
		String accessToken = getAccessToken();
		String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
		String param = String.format("access_token=%s&department_id=%s&fetch_child=1", accessToken, deptId);
		
		String response = HttpUtils.sendGet(url, param);
		System.out.println(response);
		JSONObject json = JSONObject.parseObject(response);
		
		if(json.getInteger("errcode") == 0) {
			return json.getJSONArray("userlist");
		}
		
		return null;
	}
}